package com.wude.guild;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.mc.developmentkit.utils.ToastUtil;
import com.umeng.analytics.MobclickAgent;
import com.wude.guild.imageutil.util.PhotoUtil;
import com.wude.guild.imageutil.view.PhotoViewGroup;
import com.wude.guild.servercore.mints.TitleBaseActivity;
import com.wude.http.HttpCom;
import com.wude.tools.SaveBitmapToFile;
import com.wude.tools.Utils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 评论
 */
@SuppressLint("HandlerLeak")
public class CommentActivity extends TitleBaseActivity {
    @BindView(R.id.ll_photos)
    LinearLayout ll_photos;
    @BindView(R.id.et_comment)
    EditText et_comment;

    private PhotoViewGroup viewGroup;
    private PhotoUtil photoUtil;
    private String gameId;
    private int imgCount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comment);
        ButterKnife.bind(this);

        setHeaderTitle("评论");
        TextView rightTextView = getTitleHeaderBar().getRightTextView();
        rightTextView.setText("提交");
        rightTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                commitComment();
            }
        });

        gameId = getIntent().getStringExtra("gemeId");

        photoUtil = new PhotoUtil(this);
        viewGroup = new PhotoViewGroup(this, photoUtil, 3);
        ll_photos.addView(viewGroup);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    private void commitComment() {
        String s = et_comment.getText().toString();
        List<File> files;
        if (TextUtils.isEmpty(s)) {
            ToastUtils.showLongToast("请输入评论");
            return;
        }

        List<Bitmap> bitmaps = viewGroup.getBitmaps();
        if (bitmaps != null && bitmaps.size() != 0) {
            files = SaveBitmapToFile.saveFile(bitmaps);

            for (int i = 0; i < files.size(); i++) {
                HttpCom.POSTFile(imghandler, HttpCom.getImgUpload(), Utils.getLoginUser().token, files.get(i));
            }
        } else {
            Map<String, String> dataMap = new HashMap<>();
            dataMap.put("game_id", gameId);
            dataMap.put("comment", et_comment.getText().toString());
            dataMap.put("token", Utils.getLoginUser().token);
            HttpCom.POST(handler, HttpCom.getGameDetFBPLUrl(), dataMap, false);
        }
    }

    /**
     * 提交评论图片
     */
    String imgsId = "";
    Handler imghandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    ++imgCount;
                    if (imgCount == viewGroup.getBitmaps().size()) {//全部图片上传完成
                        try {
                            JSONObject jsonObject = new JSONObject(msg.obj.toString());
                            String endimgsId = jsonObject.getString("data");
                            Map<String, String> dataMap = new HashMap<>();
                            dataMap.put("game_id", gameId);
                            dataMap.put("comment", et_comment.getText().toString());
                            dataMap.put("token", Utils.getLoginUser().token);
                            dataMap.put("imageId", imgsId + endimgsId);
                            HttpCom.POST(handler, HttpCom.getGameDetFBPLUrl(), dataMap, false);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    } else if (imgCount != viewGroup.getBitmaps().size()) {
                        try {
                            JSONObject json = new JSONObject(msg.obj.toString());
                            if (TextUtils.isEmpty(imgsId))
                                imgsId = json.getString("data") + ",";
                            else
                                imgsId = imgsId + json.getString("data") + ",";
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                    break;
                case 2:
                    ToastUtil.showToast("网络异常");
                    break;
            }
        }
    };

    /**
     * 提交评论
     */
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1://提交评论成功
                    Intent intent = new Intent();
                    intent.putExtra("updata", true);
                    setResult(1, intent);
                    finish();
                    break;
                case 2:
                    ToastUtil.showToast("网络异常");
                    break;
            }
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (viewGroup != null) {
            viewGroup.onActivityResult(requestCode, resultCode, data);
        }
    }

//    @Override
//    public void onItemClick(TCPhotoViewGroup viewGroup) {
//        this.viewGroup = viewGroup;
//    }
}
